using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameplayAchievementPopUp : MonoBehaviour
{
	public GameObject popupContainer;

	public UISprite sprite;

	public UILabel title;

	public UILabel totalAchieved;

	public float displayDuration = 5f;

	private List<GameplayAchievement> achievementsToShow = new List<GameplayAchievement>();

	private Coroutine myDisplayRoutine;

	private void Start()
	{
		GGStoryAchivementsController.instance.onAchievementAccomplished -= ShowAchievement;
		GGStoryAchivementsController.instance.onAchievementAccomplished += ShowAchievement;
	}

	public void ShowAchievement(GameplayAchievement achievement)
	{
		achievementsToShow.Add(achievement);
		if (myDisplayRoutine == null)
		{
			myDisplayRoutine = StartCoroutine(DoShowAchievement());
		}
	}

	private IEnumerator DoShowAchievement()
	{
		while (achievementsToShow.Count > 0)
		{
			GameplayAchievement achievement = achievementsToShow[0];
			achievementsToShow.RemoveAt(0);
			UITools.ChangeText(title, achievement.name);
			UITools.ChangeText(totalAchieved, achievement.achievedStagesCount + "/" + achievement.totalStages);
			popupContainer.SetActive(true);
			yield return new WaitForSeconds(displayDuration);
			popupContainer.SetActive(false);
			achievement.OnShowAchievementStageCompleted(achievement.achievedStagesCount);
		}
		myDisplayRoutine = null;
	}

	private void OnDestroy()
	{
		GGStoryAchivementsController.instance.onAchievementAccomplished -= ShowAchievement;
	}
}
